#!/bin/bash
FILE_NAME=$1
NEW_ORG_NAME=$2
STR=`sed -n '1p' ./${FILE_NAME}`
TEMP_STR=($STR)
BLOCKCHAIN_NAME=${TEMP_STR[1]}
STR=`sed -n '3p' ./${FILE_NAME}`
TEMP_STR=($STR)
CRYTPO_PATH=${TEMP_STR[1]}

ADDORG_PEER=`expr $# - 2`
#ADDORG_PEER=`expr ${ADDORG_PEER}  / 2`
STR=$@
ARG_ARR=($STR)

function addOrg(){
	line=`sed -n '/organizations:/=' ${FILE_NAME} | tail -n1`
	echo "  ${NEW_ORG_NAME}: #TAGORGS:_${NEW_ORG_NAME}
    mspid: ${NEW_ORG_NAME} #TAGORGS:_${NEW_ORG_NAME}
    cryptoPath: ${CRYTPO_PATH}/peerOrganizations/${NEW_ORG_NAME}/users/Admin@${NEW_ORG_NAME}/msp #TAGORGS:_${NEW_ORG_NAME}

    peers:    #${NEW_ORG_NAME} #TAGORGS:_${NEW_ORG_NAME}" > temp.txt
  #   for ((i=2;i<`expr ${ADDORG_PEER} + 2`;i++));do
  #   echo "      - ${ARG_ARR[i]}" >> temp.txt
 	# done
 	sed -i "${line} r temp.txt" ${FILE_NAME}
}
addOrg
